package com.yzt.zhuju.manager;

import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;
import com.yzt.zhuju.download.FileCacheManager;
import com.yzt.zhuju.network.HttpUrlManager;
import com.yzt.p_base.utils.AppInfoUtils;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;

import io.reactivex.Observable;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * author : bob
 * date   : 2022/11/29
 * desc   :
 */
public class DomainManager {

    private final String DOMAIN_URL = "https://xys-app-file.oss-cn-hangzhou.aliyuncs.com/public/javadomain/";

    private final String FILE_NAME = "domain.json";

    private String domain;

    private static DomainManager mInstance;

    public static DomainManager getInstance() {
        if (mInstance == null) {
            mInstance = new DomainManager();
        }
        return mInstance;
    }

    public String getDomain() {
        return domain;
    }

    public void init(boolean update) {
        FileCacheManager.getInstance().getDomainFileUrl(DOMAIN_URL + AppInfoUtils.getAppPackageName()
                + "/" + FILE_NAME, new FileCacheManager.OnCallBackFileUrlListener() {
            @Override
            public void getFileUrl(String fileUrl) {
                Observable.create((ObservableOnSubscribe<String>) emitter -> {
                    StringBuilder json = new StringBuilder();
                    try {
                        FileInputStream f = new FileInputStream(fileUrl);
                        BufferedReader bis = new BufferedReader(new InputStreamReader(f));
                        String line;
                        while ((line = bis.readLine()) != null) {
                            json.append(line);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    if (!TextUtils.isEmpty(json.toString())) {
                        JSONObject jsons = JSONObject.parseObject(json.toString());
                        domain = jsons.getString("domain");
                    }
                    emitter.onNext(domain);
                    emitter.onComplete();
                }).subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread()).
                        subscribe(new Observer<String>() {
                            @Override
                            public void onSubscribe(Disposable d) {
                            }

                            @Override
                            public void onComplete() {
                            }

                            @Override
                            public void onNext(String domain) {
                                if (update) {
                                    HttpUrlManager.saveHttpUrl(domain);
                                }
                            }

                            @Override
                            public void onError(Throwable e) {
                            }
                        });
            }

            @Override
            public void onProgressLength(long length) {

            }

            @Override
            public void onProgress(int progress) {

            }

            @Override
            public void loadFileFail(String fileUrl, Throwable e) {

            }
        });
    }
}
